/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    TreeNode *pre;
    bool dfs(TreeNode *root){
        if (root==NULL) return true;
        if (!dfs(root->left)) return false;
        if (pre!=NULL && pre->val>=root->val) return false;
        pre=root;
        if (!dfs(root->right)) return false;
        return true;
    }
public:
    bool isValidBST(TreeNode *root) {
        pre=NULL;
        return dfs(root);
    }
};
